package com.aidong.chat.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.chat.biz.ChatLogBiz;
import com.aidong.chat.dao.ChatLogDao;
import com.aidong.chat.entity.ChatLogEntity;


/**
 * 私信聊天service
 * @author user
 *
 */
@Service("chatLogBiz")
public class ChatLogBizImpl implements ChatLogBiz {
	
	private ChatLogDao chatLogDao;

	@Autowired
	public void setChatLogDao(ChatLogDao chatLogDao) {
		this.chatLogDao = chatLogDao;
	}

	@Override
	public void addMessage(ChatLogEntity chat) {
		chatLogDao.saveChatRelation(chat);
		chatLogDao.sendMessage(chat);
		// 消息标记位
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("idongId", chat.getRecipient());
		paraMap.put("status", 0);
		chatLogDao.eidtMemberLetterFlg(paraMap);
	}

	@Override
	public List<ChatLogEntity> getInitMessage(Map<String, Object> map) {
		List<ChatLogEntity> chats = chatLogDao.getInitMessage(map);
		
		// 把未读状态改为已读状态
		List<String> status = new ArrayList<String>();
		for (ChatLogEntity chat : chats) {
			int recipient = chat.getRecipient(); // 接收者
			int sender = Integer.parseInt(map.get("sender").toString());
			if(recipient == sender){
				status.add(chat.getCid() + "");
			}
		}
		
		if(status.size()> 0){
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("list", status);
			chatLogDao.editMessageStatus(paraMap);
		}
		
		return chats;
	}

	@Override
	public List<ChatLogEntity> getMessageList(int sender) {
		// 消息标记位
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("idongId", sender);
		paraMap.put("status", 1);
		chatLogDao.eidtMemberLetterFlg(paraMap);
		return chatLogDao.getMessageList(sender);
	}

	@Override
	public List<ChatLogEntity> getMessageListByIds(Map<String, Object> map) {
		return chatLogDao.getMessageListByIds(map);
	}

	@Override
	public List<ChatLogEntity> getUserInfo(Map<String, Object> map) {
		return chatLogDao.getUserInfo(map);
	}
	
	/**
	 * 删除聊天列表
	 * @param map
	 * @return
	 */
	@Override
	public void getMessageChat(Map<String, Object> map) {
		chatLogDao.getMessageChat(map);
	}

	/**
	 * 删除单条聊天记录
	 * @param map
	 */
	@Override
	public void delOneLetter(Map<String, Object> map) {
		chatLogDao.delOneLetter(map);
	}
}
